<template>
  <el-dialog :title="!dataForm.id ? '测评票添加' : '测评票修改'" :close-on-click-modal="false" :visible.sync="visible" @close="clearDataList()" width="800px">
    <el-container>
      <el-main style="max-height:60vh">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px">
          <el-form-item label="测评票名称" prop="sheetId">
            <el-select v-model="dataForm.sheetId" placeholder="请选择测评票名称" :disabled="control" style="width:90%;">
              <el-option v-for="(item,index) in itemData" :key="index" :label="item.sheetName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="测评票权重" prop="cppQz">
            <el-input :disabled="cppfs" type="number" v-model="dataForm.cppQz" placeholder="请输入测评票权重" oninput="value=value.replace(/[^0-9.]/g,'')" style="width:90%;"></el-input>
          </el-form-item> -->
          <el-form-item label="是否联合配置a" prop="isUnion">
            <el-radio v-model="dataForm.isUnion" label="1" disabled>是</el-radio>
            <el-radio v-model="dataForm.isUnion" label="0" disabled>否</el-radio>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
    <span slot="footer" class="dialog-footer">
      
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj, selCppList, selhdidList_new } from '@/api/ceping/eval-activity-sheet'

export default {
  data() {
    const validatecppQz = (rule, value, callback) => {
      if (value > 5000 ) {
        callback(new Error('权重不能大于5000'));
      } else {
        callback();
      }
    };
    return {
      visible: false,
      canSubmit: false,
      control: false,
      cppfs: false,
      parentR: {},
      itemData: [],
      mxblist: [],
      text: "",
      pCppfs:'',
      dataForm: {
        activityId: '',
        id: 0,
        sheetName: '',
        cppQz: '',
        sheetId: '',
        isUnion: '0',
        // cpgzBpfdx: '',
      },
      dataRule: {
        sheetId: [
          { required: true, message: '请选择测评票名称', trigger: 'blur' }
        ],
        // cppQz: [
        //   { required: true, message: '请输入测评票权重', trigger: 'blur' },
        //   { validator: validatecppQz, trigger: 'blur' }
        // ],
        isUnion: [
          { required: true, message: '是否联合配置不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    clearDataList() {
      this.dataForm = {
        activityId: '',
        id: 0,
        sheetName: '',
        cppQz: '',
        sheetId: '',
        isUnion: '0',
        // cpgzBpfdx: '',
      };
    },
    getCppName(findId, key) {
      let lResult = "";
      this.itemData.find(value => {
        if (value.id === findId) {
          lResult = value[key];
        }
      })
      return lResult;
    },
    csh() {
      this.dataForm = {
        activityId: '',
        id: 0,
        sheetName: '',
        cppQz: '',
        sheetId: '',
        isUnion: '0',
        // cpgzBpfdx: '',
      };
      this.text = "";
    },
    init(id, parentR) {
      this.csh();
      this.dataForm.id = id || 0;
      this.visible = true;
      this.canSubmit = true;
      this.control = false;
      this.parentR = parentR;
      this.pCppfs = '';
      this.pCppfs = parentR.hdCppfs;

      selCppList(parentR.hdCppfs).then(res => {
        this.itemData = res.data.data;
      });
      selhdidList_new(parentR.id).then(res => {
        this.mxblist = res.data.data;
      });
      this.dataForm.activityId = parentR.id;
      // if (parentR.hdCppfs == '单票' || parentR.hdCppfs == '部门与个人') {
      if (parentR.hdCppfs == '1') {
        this.dataForm.cppQz = 100;
        this.cppfs = true;
      } else {
        this.cppfs = false;
      }
      if (parentR.hdCppfs == '1'){
        this.dataForm.isUnion = '1';
      }

      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.control = true;
          getObj(this.dataForm.id).then(response => {
            this.dataForm = response.data.data;
            this.dataForm.isUnion = response.data.data.isUnion + "";
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          this.dataForm.sheetName = this.getCppName(this.dataForm.sheetId, "sheetName");
          // this.dataForm.cpgzBpfdx = this.getCppName(this.dataForm.cppId,"cpgzBpfdx");
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.closeAll();
              this.$notify.success('修改成功');
              this.visible = false;
              this.$emit('refreshSubList');
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            if (this.verification()) {
              this.$notify.closeAll();
              this.$notify.error(this.text);
              this.canSubmit = true;
            } else {
              addObj(this.dataForm).then(data => {
                this.$notify.closeAll();
                this.$notify.success('添加成功');
                this.visible = false;
                this.$emit('refreshSubList');
              }).catch(() => {
                this.canSubmit = true;
              });
            }

          }
        }
      })
    },
    //数据控制验证
    verification() {
      if (this.parentR.hdCppfs == "1") {
        if (this.mxblist.length > 0) {
          this.text = "单票只能选择一个测评票。";
          return true;
        }
      } else if (this.parentR.hdCppfs == "2" || this.parentR.hdCppfs == "3" || this.parentR.hdCppfs == "4") {
        for (var a = 0; a < this.mxblist.length; a++) {
          if (this.mxblist[a].sheetId == this.dataForm.sheetId) {
            this.text = "该测评票已被选择，无法重复添加。";
            return true;
          }
        }
      }
      // if (this.parentR.hdCppfs == "部门与个人") {
      //   if (this.mxblist.length == 0 && this.getCppName(this.dataForm.cppId, "cpgzBpfdx") == "个人") {
      //     this.text = "该关联类型必须先添加部门类别测评票。";
      //     return true;
      //   }
      //
      //   for (var a = 0; a < this.mxblist.length; a++) {
      //     if (this.getCppName(this.mxblist[a].cppId, "cpgzBpfdx") == "部门" && this.getCppName(this.dataForm.cppId, "cpgzBpfdx") == "部门") {
      //       this.text = "该关联类型只能选择一种部门测评票。";
      //       return true;
      //     }
      //     if (this.getCppName(this.mxblist[a].cppId, "cpgzBpfdx") == "个人" && this.getCppName(this.dataForm.cppId, "cpgzBpfdx") == "个人") {
      //       this.text = "该关联类型只能选择一种个人测评票。";
      //       return true;
      //     }
      //   }
      // }
      return false;
    },

  }
}
</script>
